package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.Response;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@SuppressWarnings("all")
public class SendSms {
    private static final Logger log = LoggerFactory.getLogger(SendSms.class);

    //腾讯云短信API参数
    private final static String CHARSET = "UTF-8";
    // 密钥参数，云API密匙查询: https://console.cloud.tencent.com/cam/capi
//    private final static String SECRET_ID = "AKIDJFSUKxQn7IJ9St77rMcgW9WeXEk3861i";
//    private final static String SECRET_KEY = "Z6PWQL3WnvtmbNJ6i6KK7iASXO1Wpxvh";
//
//    private final static String SDKAPPID = "1400698278";
//    private final static String SIGNNAME = "暮云网络";
//    private final static String TEMPLATEID = "1455110";
//
//    /**饰品发货提醒短信*/
//    private final static String SDKAPPID_GOODS = "1400698278";
//    private final static String SIGNNAME_GOODS = "暮云网络";
//    private final static String TEMPLATEID_GOODS = "1635520";

    private final static String SECRET_ID = "AKIDMcmohjo0KvR1gGi3LQVl3Sp7RlMo46uV";
    private final static String SECRET_KEY = "Wq175xkPvW4QZdZZQME0hSEoIg3IJ7h3";

    private final static String SDKAPPID = "1400881771";
    private final static String SIGNNAME = "亚特洛网络";
    private final static String TEMPLATEID = "2043925";

    /**饰品发货提醒短信*/
    private final static String SDKAPPID_GOODS = "1400881771";
    private final static String SIGNNAME_GOODS = "亚特洛网络";
    private final static String TEMPLATEID_GOODS = "2075213";


//    public static SendSmsResponse sendCode(String phone,String code) throws ClientException {
//        final String product = "Dysmsapi";//短信API产品名称（短信产品名固定，无需修改）
//        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名（接口地址固定，无需修改）
//        final String accessKeyId = "LTAI5tLxpK343WrXx151bheb";//你的accessKeyId,参考本文档步骤2
//        final String accessKeySecret = "wWCrcSpJZnOuJWLYiIQ3kcs245wTaE";
//
//        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
//        IAcsClient client = new DefaultAcsClient(profile);
//        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
//        IAcsClient acsClient = new DefaultAcsClient(profile);
//
//        SendSmsRequest request = new SendSmsRequest();
//        request.setSysMethod(MethodType.POST);
//        request.setPhoneNumbers(phone);
//        //必填:短信签名-可在短信控制台中找到
//        request.setSignName("咕噜盒子盲盒");
//        //必填:短信模板-可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
//        request.setTemplateCode("SMS_234153459");
//        request.setTemplateParam("{\"code\":\"" + code + "\"}");
//        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
//        return sendSmsResponse;
//    }

    public static Map sendCode(String phone, String code) throws TencentCloudSDKException {
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的  第二个参数是地域信息
        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        SendSmsRequest req = new SendSmsRequest();
        //设置固定的参数
        req.setSmsSdkAppId(SDKAPPID);
        req.setSignName(SIGNNAME);
        req.setTemplateId(TEMPLATEID);
        //设置发送相关的参数
        String[] phoneNumberSet1 = {phone};
        req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号
        String[] templateParamSet1 = {code};
        req.setTemplateParamSet(templateParamSet1);//发送验证码
        // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
        SendSmsResponse resp = client.SendSms(req);
        String s = SendSmsResponse.toJsonString(resp);
        System.out.println(s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray sendStatusSet = jsonObject.getJSONArray("SendStatusSet");
        return (Map) sendStatusSet.get(0);
    }


    public static void sendDeliverGoodsCode(String phone, String goodsName){
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            //设置固定的参数
            req.setSmsSdkAppId(SDKAPPID_GOODS);
            req.setSignName(SIGNNAME_GOODS);
            req.setTemplateId(TEMPLATEID_GOODS);
            String[] phoneNumberSet1 = {phone};
            req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号
            String[] templateParamSet1 = {goodsName};
            req.setTemplateParamSet(templateParamSet1);//发送验证码
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            String s = SendSmsResponse.toJsonString(resp);
            JSONObject jsonObject = JSONObject.parseObject(s);
            JSONArray sendStatusSet = jsonObject.getJSONArray("SendStatusSet");
            Map map = (Map) sendStatusSet.get(0);
            if (map.get("Code").equals("InternalError.Timeout")) {
                log.error("饰品发货提醒短信发送失败：请求下发短信超时！");
            } else if (map.get("Code").equals("UnauthorizedOperation.RequestIpNotInWhitelist")) {
                log.error("饰品发货提醒短信发送失败：请求IP不在白名单中！");
            } else if (map.get("Code").equals("InvalidParameterValue.IncorrectPhoneNumber")) {
                log.error("饰品发货提醒短信发送失败：手机号格式错误！");
            } else if (map.get("Code").equals("LimitExceeded.DailyLimit")) {
                log.error("饰品发货提醒短信发送失败：当日下发条数超过设定的上限！");
            } else if (map.get("Code").equals("LimitExceeded.PhoneNumberDailyLimit")) {
                log.error("饰品发货提醒短信发送失败：当前手机号发送短信条数超过当日上限！");
            } else if (map.get("Code").equals("LimitExceeded.PhoneNumberOneHourLimit")) {
                log.error("饰品发货提醒短信发送失败：当前手机号1小时内发送短信条数超过上限！");
            }
        } catch (TencentCloudSDKException e) {
            log.error("饰品发货提醒短信发送异常：{}",e);
        }
    }
}
